<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 文章管理
 */
class PostController extends BaseController
{
    /**
     * 文章列表
     * @return [type] [description]
     */
    public function index($key="")
    {
        if($key == ""){
            $model = D('PostView'); 
        }else{
            $where['post.title'] = array('like',"%$key%");
            $where['member.username'] = array('like',"%$key%");
            $where['_logic'] = 'or';
            $model = D('PostView')->where($where); 
        } 
        
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $post = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('post.id DESC')->select();
        $this->assign('model', $post);
        $this->assign('page',$show);
        $this->display();     
    }
    /**
     * 添加文章
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
        	//$this->assign("category",getSortedCategory(M('category')->select()));
            $this->display();
        }
        if (IS_POST) {
            $image = '';
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      C('POST_PATH'); // 设置附件上传根目录
            // 取得成功上传的文件信息
            $info = $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                $image = $info['image']['savepath'].$info['image']['savename'];
            }

            //如果用户提交数据
            $model = M("Post");
            $model->time = time();
            $model->title = I('post.title');
            $model->user_id = 1;
            $model->image = $image;
            $model->describe = I('post.describe');
            $model->content = I('post.content');

            if ($model->add()) {
                $this->success("添加成功", U('post/index'));
            } else {
                $this->error("添加失败");
            }

        }
    }
    /**
     * 更新文章信息
     * @param  [type] $id [文章ID]
     * @return [type]     [description]
     */
    public function update($id)
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->post = M('post')->where('id='.$id)->find();

            $this->display();
        }
        if (IS_POST) {
            $id = I('post.post_id');
            //判断是否上传图片
            if($_FILES['file']['name']){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      C('POST_PATH'); // 设置附件上传根目录
                // 取得成功上传的文件信息
                $info = $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    $image = $info['file']['savepath'].$info['file']['savename'];
                }
                // 保存当前数据对象
                $model = M("Post");
                $model->title = I('post.title');
                $model->user_id = 1;
                $model->image = $image;
                $model->describe = I('post.describe');
                $model->content = I('post.content');

                if ($model->where(array('id' => $id))->save()) {
                    $this->success("编辑成功");
                } else {
                    $this->error("编辑失败");
                }

            }else{
                //如果用户提交数据
                $model = M("Post");
                $model->title = I('post.title');
                $model->user_id = 1;
                $model->describe = I('post.describe');
                $model->content = I('post.content');

                if ($model->where(array('id' => $id))->save()) {
                    $this->success("编辑成功");
                } else {
                    $this->error("编辑失败");
                }
            }
        }
    }
    /**
     * 删除文章
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $model = M('post');
        $result = $model->where("id=".$id)->delete();
        if($result){
            $this->success("删除成功", U('post/index'));
        }else{
            $this->error("删除失败");
        }
    }
}
